﻿using SuperX.Common;
using SuperX.Common.Collection;
using System;
using System.Collections.Generic;
using System.Text;

namespace SuperX.DataService.Abstract
{
    public interface IGroup : IDisposable, IObjCollection
    {
        bool IsActive { get; set; }
        short ID { get; }
        int UpdateRate { get; set; }
        float DeadBand { get; set; }
        IDriver IDriverParent { get; }
        IDataServer Server { get; }

        IEnumerable<IIOTag> Items { get; }

        string ConfigName { get; set; }

        bool AddItems(IList<TagConfig> items);

        bool AddTags(IEnumerable<IIOTag> tags);

        bool RemoveItems(params IIOTag[] items);

        bool SetActiveState(bool active, params long[] items);

        IIOTag FindItemByAddress(DeviceAddress addr);

        ItemData<int> ReadInt32(DeviceAddress address, DataSource source = DataSource.Cache);

        ItemData<uint> ReadUInt32(DeviceAddress address, DataSource source = DataSource.Cache);

        ItemData<short> ReadInt16(DeviceAddress address, DataSource source = DataSource.Cache);

        ItemData<ushort> ReadUInt16(DeviceAddress address, DataSource source = DataSource.Cache);

        ItemData<byte> ReadByte(DeviceAddress address, DataSource source = DataSource.Cache);

        ItemData<float> ReadFloat(DeviceAddress address, DataSource source = DataSource.Cache);

        ItemData<bool> ReadBool(DeviceAddress address, DataSource source = DataSource.Cache);

        ItemData<string> ReadString(DeviceAddress address, DataSource source = DataSource.Cache);

        ItemData<dynamic> ReadDynamic(DeviceAddress address, DataSource source = DataSource.Cache);

        int WriteInt32(DeviceAddress address, int value);

        int WriteUInt32(DeviceAddress address, uint value);

        int WriteInt16(DeviceAddress address, short value);

        int WriteUInt16(DeviceAddress address, ushort value);

        int WriteFloat(DeviceAddress address, float value);

        int WriteString(DeviceAddress address, string value);

        int WriteBit(DeviceAddress address, bool value);

        int WriteBits(DeviceAddress address, byte value);

        int WriteDouble(DeviceAddress address, double value);

        int WriteLong(DeviceAddress address, long value);

        event DataChangeEventHandler DataChange;
    }

    public class DataChangeEventArgs : EventArgs
    {
        public int TransactionID;
    }

    public class WriteCompleteEventArgs : EventArgs
    {
        public WriteCompleteEventArgs(int transactionID, short[] pIds, int[] errors)
        {
            this.TransactionID = transactionID;
            this.Values = pIds;
            this.Errors = errors;
        }

        public int TransactionID;
        public short[] Values;
        public int[] Errors;
    }

    public delegate void DataChangeEventHandler(object sender, DataChangeEventArgs e);

    public delegate void ReadCompleteEventHandler(object sender, DataChangeEventArgs e);

    public delegate void WriteCompleteEventHandler(object sender, WriteCompleteEventArgs e);
}